home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_055 / newstartups / twstartup.asm < prev    next >
Assembly Source File  |  1992-05-06  |  11KB  |  460 lines

  1. * ==========================
  2. * amiga.dev/programs #12, from cscheppner, 11195 chars, Tue Feb 24 17:54:45 1987
  3. * --------------------------
  4. * TITLE:  TWstartup.asm --- Modified Astartup, opens Amiga stdio under WB
  5.  
  6. *******************************************************************
  7. * TWStartup.asm  - Amiga Startup opens stdio window on WB startup *
  8. *                  using TWspec defined in application            *
  9. *       example:   char TWspec[] = "CON:30/30/200/150/MyProgram"; *
  10. *                   ( OR  char TWspec = "";  for no window )      *
  11. *                                                                 *
  12. * Modified be C. Scheppner (08/03/86) from the following source:  *
  13. ******************************************************************* 
  14. *                                                                 *
  15. *   C Program Startup/Exit (Combo Version: CLI and WorkBench)     *
  16. *                                                                 *
  17. *******************************************************************
  18. *
  19. * Source Control:
  20. * --------------
  21. *
  22. * $Header: startup.asm,v 1.1 86/08/25 12:50:07 root Exp $
  23. *
  24. * $Locker: root $
  25. *
  26. * $Log:   startup.asm,v $
  27. * Revision 1.1  86/08/25  12:50:07  root
  28. * Initial revision
  29. *
  30. * Revision 33.6  86/06/12  10:34:36  neil
  31. * changed so any version of dos is OK
  32. * Revision 33.5  86/06/11  19:03:47  neil
  33. * fixed CloseLibrary if open of dos failed
  34. * Revision 33.4  86/06/09  16:48:59  neil
  35. * now processes escapes also
  36. * Revision 33.3  86/06/09  16:18:12  neil
  37. * another checkpoint -- quoted strings now work
  38. * Revision 1.1  85/11/23  13:49:01  neil
  39. * Initial revision
  40. *
  41. ************************************************************************
  42.  
  43.  
  44. ******* Included Files *************************************************
  45.  
  46.    INCLUDE "exec/types.i"
  47.    INCLUDE "exec/alerts.i"
  48.    INCLUDE "exec/nodes.i"
  49.    INCLUDE "exec/lists.i"
  50.    INCLUDE "exec/ports.i"
  51.    INCLUDE "exec/libraries.i"
  52.    INCLUDE "exec/tasks.i"
  53.    INCLUDE "libraries/dos.i"
  54.    INCLUDE "libraries/dosextens.i"
  55.    INCLUDE "workbench/startup.i"
  56.  
  57.  
  58. ******* Imported *******************************************************
  59.  
  60. xlib   macro
  61.    xref   _LVO\1
  62.    endm
  63.  
  64.    xref   _AbsExecBase
  65.    xref   _Input
  66.    xref   _Output
  67.  
  68.    xref   _main         ; C code entry point
  69.  
  70.    xref   _TWspec       ; con: spec in application for WB stdio window
  71.  
  72.    xlib   Alert
  73.    xlib   FindTask
  74.    xlib   Forbid
  75.    xlib   GetMsg
  76.    xlib   OpenLibrary
  77.    xlib   CloseLibrary
  78.    xlib   ReplyMsg
  79.    xlib   Wait
  80.    xlib   WaitPort
  81.  
  82.    xlib   CurrentDir
  83.    xlib   Open
  84.    xlib   Close
  85.  
  86. ******* Exported *******************************************************
  87.  
  88.    xdef   _SysBase
  89.    xdef   _DOSBase
  90.  
  91.    xdef   _errno
  92.    xdef   _stdin
  93.    xdef   _stdout
  94.    xdef   _stderr
  95.  
  96.    xdef   _exit         ; standard C exit function
  97.    xdef   _WBenchMsg
  98.  
  99.  
  100. callsys   macro
  101.    CALLLIB   _LVO\1
  102.    endm
  103.  
  104.  
  105. ************************************************************************
  106. *
  107. *   Standard Program Entry Point
  108. *
  109. ************************************************************************
  110. *
  111. *   main (argc, argv)
  112. *      int  argc;
  113. *      char *argv[]; 
  114. *
  115. ************************************************************************
  116.  
  117. startup:            ; reference for Wack users
  118.       move.l   sp,initialSP   ; initial task stack pointer
  119.       move.l   d0,dosCmdLen
  120.       move.l   a0,dosCmdBuf
  121.  
  122.       ;------ get Exec's library base pointer:
  123.       move.l   _AbsExecBase,a6
  124.       move.l   a6,_SysBase
  125.  
  126.       ;------ get the address of our task
  127.       suba.l   a1,a1
  128.       callsys   FindTask
  129.       move.l   d0,a4
  130.  
  131.       ;------ are we running as a son of Workbench?
  132.       tst.l   pr_CLI(A4)
  133.       beq   fromWorkbench
  134.  
  135. ;=======================================================================
  136. ;====== CLI Startup Code ===============================================
  137. ;=======================================================================
  138. fromCLI:
  139.       ;------   attempt to open DOS library:
  140.       bsr   openDOS
  141.  
  142.       ;------ find command name:
  143.       suba.l   a0,a0
  144.       move.l   pr_CLI(a4),d0
  145.       lsl.l   #2,d0      ; bcpl pointer conversion
  146.       move.l   cli_CommandName(a0,d0.l),d0
  147.       lsl.l   #2,d0      ; bcpl pointer conversion
  148.  
  149.       ;------ create buffer and array:
  150.       movem.l   a2/a3,-(sp)
  151.       lea   argvBuffer,a2
  152.       lea   argvArray,a3
  153.  
  154.       ;------ fetch command name:
  155.       move.l   d0,a0
  156.       moveq.l   #0,d0
  157.       move.b   (a0)+,d0   ; size of command name
  158.       clr.b   0(a0,d0.l)   ; terminate the string
  159.       move.l   a0,(a3)+
  160.  
  161.       ;------   collect parameters:
  162.       move.l   dosCmdLen,d0
  163.       move.l   dosCmdBuf,a0
  164.  
  165.       ;------ null terminate the string, eat trailing garbage
  166.       lea   0(a0,d0.l),a1
  167. stripjunk:
  168.       cmp.b   #' ',-(a1)
  169.  
  170.       ;-- jimm: 8/25/86: per kodiak's recommendation
  171.       ; bls.s   stripjunk
  172.       dbhi   D0,stripjunk
  173.  
  174.       clr.b   1(a1)
  175.  
  176. newarg:
  177.       ;------ skip spaces
  178.       move.b   (a0)+,d1
  179.       beq.s   parmExit
  180.       cmp.b   #' ',d1
  181.       beq.s   newarg
  182.       cmp.b   #9,d1         ; tab
  183.       beq.s   newarg
  184.  
  185.       ;------ push address of the next parameter
  186.       move.l   a2,(a3)+
  187.  
  188.       ;------ process quotes
  189.       cmp.b   #'"',d1
  190.       beq.s   doquote
  191.  
  192.       ;------ copy the parameter in
  193.       move.b   d1,(a2)+
  194.  
  195. nextchar:
  196.       ;------ null termination check
  197.       move.b   (a0)+,d1
  198.       beq.s   parmExit
  199.       cmp.b   #' ',d1
  200.       beq.s   endarg
  201.  
  202.       move.b   d1,(a2)+
  203.       bra.s   nextchar
  204.  
  205. endarg:
  206.       clr.b   (a2)+
  207.       bra.s   newarg
  208.  
  209. doquote:
  210.       ;------ process quoted strings
  211.       move.b   (a0)+,d1
  212.       beq.s   parmExit
  213.       cmp.b   #'"',d1
  214.       beq.s   endarg
  215.  
  216.       ;------ '*' is the BCPL escape character
  217.       cmp.b   #'*',d1
  218.       bne.s   addquotechar
  219.  
  220.       move.b   (a0)+,d1
  221.       cmp.b   #'N',d1
  222.       beq.s   1$
  223.       cmp.b   #'n',d1
  224.       bne.s   2$
  225.  
  226. 1$:
  227.       ;------ got a *N -- turn into a newline
  228.       moveq   #10,d1
  229.       bra.s   addquotechar
  230.  
  231. 2$:
  232.       cmp.b   #'E',d1
  233.       beq.s   3$
  234.       cmp.b   #'e',d1
  235.       bne.s   addquotechar
  236.  
  237. 3$:
  238.       ;------ got a *E -- turn into a escape
  239.       moveq   #27,d1
  240.  
  241. addquotechar:
  242.       move.b   d1,(a2)+
  243.       bra.s   doquote
  244.  
  245. parmExit:
  246.       ;------ all done -- null terminate the baby
  247.       clr.b   (a2)
  248.       clr.l   (a3)
  249.  
  250.       ;------ compute the # of arguments
  251.       move.l   #dosCmdBuf,d0
  252.       sub.l   a3,d0
  253.       not.l   d0
  254.       lsr.l   #2,d0
  255.  
  256.       movem.l   (sp)+,a2/a3
  257.       pea   argvArray
  258.       move.l   d0,-(sp)
  259.  
  260.  
  261. *
  262. *  The above code relies on the end of line containing a control
  263. *  character of any type, i.e. a valid character must not be the
  264. *  last.  This fact is ensured by DOS.
  265. *
  266.       
  267.  
  268.       ;------ get standard input handle:
  269.       jsr   _Input
  270.       move.l   d0,_stdin
  271.  
  272.       ;------ get standard output handle:
  273.       jsr   _Output
  274.       move.l   d0,_stdout
  275.       move.l   d0,_stderr
  276.  
  277.       ;------ call C main entry point
  278.       jsr   _main
  279.  
  280.       ;------ return success code:
  281.       moveq.l   #0,D0
  282.       move.l   initialSP,sp   ; restore stack ptr
  283.       rts
  284.  
  285. ;=======================================================================
  286. ;====== Workbench Startup Code =========================================
  287. ;=======================================================================
  288. fromWorkbench:
  289.       ;------ open the DOS library:
  290.       bsr   openDOS
  291.  
  292.       ;------ we are now set up.  wait for a message from our starter
  293.       bsr   waitmsg
  294.  
  295.       ;------ save the message so we can return it later
  296.       move.l   d0,_WBenchMsg
  297.  
  298.       ;------ push the message on the stack for wbmain
  299.       move.l   d0,-(SP)
  300.       clr.l   -(SP)      indicate: run from Workbench
  301.  
  302.       ;------ get the first argument
  303.       move.l   d0,a2
  304.       move.l   sm_ArgList(a2),d0
  305.       beq.s   docons
  306.  
  307.       ;------ and set the current directory to the same directory
  308.       move.l   _DOSBase,a6
  309.       move.l   d0,a0
  310.       move.l   wa_Lock(a0),d1
  311.       callsys   CurrentDir
  312.  
  313. * ==============================================
  314. * ============ Opens stdio window ==============
  315. * ==============================================
  316.  
  317. docons: 
  318.       lea.l    _TWspec,a0
  319.       cmp.b    #0,(a0)           No window if TWspec is null string
  320.       beq.s    domain
  321.  
  322.    ;------ open up the file
  323.       move.l   a0,d1
  324.       move.l   #MODE_OLDFILE,d2 
  325.       move.l   _DOSBase,a6
  326.       callsys   Open 
  327.  
  328.    ;------ set the C input and output descriptors 
  329.       cmp.l    #0,d0
  330.       beq.s    exit2
  331.       move.l   d0,_stdin 
  332.       move.l   d0,_stdout 
  333.       move.l   d0,_stderr 
  334.  
  335.    ;------ set the console task (so Open( "*", mode ) will work 
  336.    ;   waitmsg has left the task pointer in A4 for us 
  337.       move.l   d0,pr_CIS(A4)
  338.       move.l   d0,pr_COS(A4)
  339.       lsl.l    #2,d0 
  340.       move.l   d0,a0 
  341.       move.l   fh_Type(a0),pr_ConsoleTask(A4) 
  342.  
  343.  
  344. domain: 
  345.       jsr   _main
  346.       moveq.l   #0,d0      Successful return code
  347.       bra.s   exit2
  348.  
  349.  
  350. ************************************************************************
  351. *
  352. *   C Program Exit Function
  353. *
  354. ************************************************************************
  355. *
  356. *  Warning: this function really needs to do more than this.
  357. *
  358. ************************************************************************
  359.  
  360. _exit:
  361.       move.l   4(SP),d0   ; extract return code
  362. exit2:
  363.    ;------ Close ToolWin if we have one:
  364.       tst.l   _WBenchMsg 
  365.       beq.s   noTW
  366.       move.l  _stdin,d1
  367.       bmi.s   noTW
  368.       move.l  _DOSBase,a6
  369.       callsys Close
  370.  
  371. noTW:
  372.       move.l  initialSP,SP   ; restore stack pointer
  373.       move.l   d0,-(SP)   ; save return code
  374.  
  375.       ;------ close DOS library:
  376.       move.l   _AbsExecBase,A6
  377.       move.l   _DOSBase,d0
  378.       beq.s   1$
  379.       move.l   d0,a1
  380.       callsys   CloseLibrary
  381. 1$:
  382.  
  383.       ;------ if we ran from CLI, skip workbench cleanup:
  384.       tst.l   _WBenchMsg
  385.       beq.s   exitToDOS
  386.  
  387.       ;------ return the startup message to our parent
  388.       ;------   we forbid so workbench can't UnLoadSeg() us
  389.       ;------   before we are done:
  390.       callsys Forbid
  391.       move.l   _WBenchMsg,a1
  392.       callsys   ReplyMsg
  393.  
  394.       ;------ this rts sends us back to DOS:
  395. exitToDOS:
  396.       move.l   (SP)+,d0
  397.       rts
  398.  
  399.  
  400. ;-----------------------------------------------------------------------
  401. noDOS:
  402.       ALERT   (AG_OpenLib!AO_DOSLib)
  403.       moveq.l   #100,d0
  404.       bra.s   exit2
  405.  
  406.  
  407. ;-----------------------------------------------------------------------
  408. ; This routine gets the message that workbench will send to us
  409. ; called with task id in A4
  410.  
  411. waitmsg:
  412.       lea   pr_MsgPort(A4),a0     * our process base
  413.       callsys   WaitPort
  414.       lea   pr_MsgPort(A4),a0     * our process base
  415.       callsys GetMsg
  416.       rts
  417.  
  418. ;-----------------------------------------------------------------------
  419. ;  Open the DOS library:
  420.  
  421. openDOS
  422.       lea   DOSName(pc),A1
  423.       moveq   #0,d0
  424.       callsys OpenLibrary
  425.       move.l   D0,_DOSBase
  426.       beq   noDOS
  427.       rts
  428.  
  429. DOSName      DOSNAME
  430.  
  431. ************************************************************************
  432.  
  433.    DATA
  434.  
  435. ************************************************************************
  436.  
  437. VerRev      dc.w   33,1
  438.  
  439. _SysBase   dc.l   0
  440. _DOSBase   dc.l   0
  441.  
  442. _errno      dc.l   0
  443. _stdin      dc.l   -1
  444. _stdout      dc.l   -1
  445. _stderr      dc.l   -1
  446.  
  447. initialSP   dc.l   0
  448. _WBenchMsg   dc.l   0
  449.  
  450. dosCmdLen   dc.l   0
  451. dosCmdBuf   dc.l   0
  452.  
  453. argvArray   ds.l   32
  454. argvBuffer   ds.b   256
  455.